package ilarele.planner.Activities;

import ilarele.planner.Adapters.GalleryTaskAdapter;
import ilarele.planner.Adapters.GridViewTaskAdapter;
import ilarele.planner.Project.Project;
import ilarele.planner.Project.Task;
import ilarele.planner.Scrolls.MyHorizontalScroll;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewTreeObserver.OnScrollChangedListener;
import android.widget.Gallery;
import android.widget.GridView;

public class AnPlanTest2 extends Activity implements
		OnScrollChangedListener
{

	// menu entries
	private static final int MENU_ADD_TASK = 0;

	// GUI
	private GridViewTaskAdapter gridview_task_adapter;
	private GalleryTaskAdapter gallery_task_adapter;
	private MyHorizontalScroll listview_h_scroll;


	// content
	private Project project;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		project = new Project();
		gallery_task_adapter = new GalleryTaskAdapter(this, R.layout.task_layout, getTasks(),
				project);
		gridview_task_adapter = new GridViewTaskAdapter(this, R.layout.task_layout, getTasks(),
				project);
		


	    Gallery gallery = (Gallery) findViewById(R.id.gallery);
	    gallery.setAdapter(gallery_task_adapter);
	    
	    GridView gridview = (GridView) findViewById(R.id.gridview);
	    gridview.setAdapter(gridview_task_adapter);
	    
	    
		addDummyTasks(10);

//		gridview.setOnScrollListener(new ListViewOnScrollListener());

	}

	private void addDummyTasks(int n)
	{
		for (int i = 0; i < n; i++)
		{
			Calendar start = Calendar.getInstance();
			start.set(110, 9, 1 + i);
			Task new_dummy = new Task(getTasks().size(), start);
			getTasks().add(new_dummy);
			gallery_task_adapter.notifyDataSetChanged();
			gridview_task_adapter.notifyDataSetChanged();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		menu.add(0, MENU_ADD_TASK, 0, R.string.menu_add_task).setIcon(
				android.R.drawable.ic_menu_add);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		super.onOptionsItemSelected(item);
		switch (item.getItemId())
		{
		case MENU_ADD_TASK:
			Task new_dummy = new Task(getTasks().size());
			getTasks().add(new_dummy);
			gallery_task_adapter.notifyDataSetChanged();
			gridview_task_adapter.notifyDataSetChanged();
			return true;
		}

		return false;
	}

	private ArrayList<Task> getTasks()
	{
		return project.getAllTasks();
	}

	@Override
	public void onScrollChanged()
	{
//		Log.d("log", "onScrollChanged");

	}


}
